// pages/login/login.js
import httpUtil from '../../utils/httpUtil.js'
var token = wx.getStorageSync("token")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function(res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true
          })
        } else {
          that.setData({
            outline: false,
          })
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
        })
      }
    })
  },
  onGotUserInfo: function(e) {
    var that = this;
    if (e == undefined) {

    } else {
      var req = {
        openid: wx.getStorageSync("openId"),
        nickname: e.detail.userInfo.nickName,
        headimgurl: e.detail.userInfo.avatarUrl,
        sex: e.detail.userInfo.gender,
        province: e.detail.userInfo.province,
        city: e.detail.userInfo.city,
      }
      httpUtil.post("/crm/myuserinfo", req)
        .then(function(res) {
          if (res.data.status == 200) {
            wx.setStorageSync("imgUrl", res.data.data.headimgurl);
            wx.setStorageSync("nickName", res.data.data.nickname);
            wx.setStorageSync("userId", res.data.data.id);
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          } else {
            wx.showToast({
              title: '' + res.data.message + '',
            })
          }
        }).catch(function(res) {
          console.log(res);
        });
    }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})